package com.villaday.idf.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;

import com.villaday.idf.R;
import com.villaday.idf.base.AbsRecyclerViewAdapter;
import com.villaday.idf.base.OnAdapterCallbackListener;
import com.villaday.idf.model.RecommendEntity;
import com.villaday.idf.model.RefreshtokenEntity;
import com.villaday.idf.model.VipsMemberOrders;
import com.villaday.idf.util.DateUtils;
import com.villaday.idf.util.PhoneUtil;
import com.villaday.idf.util.SharedPreferencesUtil;

import java.text.ParseException;

/**
 * Vip客户订单适配器，单独使用
 */

public class CustomerOrderAdapter extends AbsRecyclerViewAdapter<VipsMemberOrders.BodyBean.ListBean> {
    private OnAdapterCallbackListener onAdapterCallbackListener;
    private View.OnClickListener onClickListener;
    private final String roleType;

    public CustomerOrderAdapter(Context context, OnAdapterCallbackListener onAdapterCallbackListener) {
        super(context, R.layout.recyclerview_customer_order, R.layout.item_page_loading,
                R.layout.item_page_bottom);
        this.onAdapterCallbackListener = onAdapterCallbackListener;
        RefreshtokenEntity.BodyBean roleInfo =
                SharedPreferencesUtil.getRoleInfo(context);
        roleType = roleInfo.getMember().getType();

    }

    @Override
    public int getItemType(VipsMemberOrders.BodyBean.ListBean d) {
        return d.getMyType();
    }

    @Override
    public void onBindHolder(AbsRecyclerViewAdapter.RecyclerViewHolder holder, VipsMemberOrders.BodyBean.ListBean d, int position) {
        switch (d.getMyType()) {

            case 0: {
                holder.bindSimpleDraweeView(R.id.sdv_head, TextUtils.isEmpty(d.getMember().getHeaderUrl()) ? "" : d.getMember().getHeaderUrl())
                        .bindTextView(R.id.tv_nick_name, TextUtils.isEmpty(d.getMember().getNickName()) ? "" : d.getMember().getNickName())
                        .bindTextView(R.id.tv_name, TextUtils.isEmpty(d.getMember().getPhone()) ? "" : PhoneUtil.getMobile(d.getMember().getPhone()))
                        .bindTextView(R.id.tv_order_type, d.getCreateTime())
                        .bindSimpleDraweeView(R.id.sdv, d.getImageUrl())
                        .bindTextView(R.id.tv_title, d.getName())
                        .bindTextView(R.id.detail, "¥" + PhoneUtil.handleDecimal2((Integer.parseInt(d.getCurrentPrice()) / 100.0) + ""))
                        .bindTextView(R.id.tv_order_status, d.getStatusText())
                        .bindTextView(R.id.tv_price, d.getTradeId())
                        .bindTextView(R.id.tv_commission, "我的佣金：¥" + PhoneUtil.handleDecimal2NO((Integer.parseInt(d.getBrokenPrice()) / 100.0) + ""))
                        .setOnClickListener(R.id.tv_into_goods);
                LinearLayout stayAwayLayout = (LinearLayout) holder.getView(R.id.ll_stay_away_layout);
                if (TextUtils.isEmpty(d.getCheckInDate()) && TextUtils.isEmpty(d.getCheckOutDate())) {
                    stayAwayLayout.setVisibility(View.GONE);
                } else {
                    long days = 0;
                    stayAwayLayout.setVisibility(View.GONE);
                    String start = d.getCheckInDate().split(" ")[0];
                    String end = d.getCheckOutDate();
                    try {
                        days = DateUtils.getDatesDays(end, start);
                    } catch (ParseException e) {
                        e.printStackTrace();
                    }
                    String[] startDate = start.split("-");
                    String[] endDate = end.split("-");
                    String value = startDate[1] + "月" + startDate[2] + "日-" + endDate[1] + "月" + endDate[2] + "日(" + days + "晚)";
                    holder.bindTextView(R.id.tv_stay_away_date, value);
                }
            }
            break;

            case 1: {
                onAdapterCallbackListener.onCallback();
            }
            break;


        }
    }
}
